﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Globalization;

namespace MHP3rd
{
    /// <summary>
    /// App.xaml 的互動邏輯
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            ShowStartUp();
            if (loadLanguage()) base.OnStartup(e);
            else base.Shutdown();
        }

        private void ShowStartUp()
        {
            SplashScreen s = new SplashScreen("res/start.png");
            s.Show(false, true);
            s.Close(new TimeSpan(0, 0, 5));
        }

        private bool loadLanguage()
        {
            bool lb_load = false;
            ResourceDictionary lrd_rd = null;
            string ls_lang = CultureInfo.CurrentCulture.Name;


        Label:
            try
            {
                lrd_rd = Application.LoadComponent(new Uri("lang/" + ls_lang + ".xaml", UriKind.Relative)) as ResourceDictionary;
            }
            catch
            {
                if (ls_lang.Equals("zh-tw"))
                {
                    string ls_msg = (CultureInfo.CurrentCulture.Name.ToLower().Equals("zh-tw") ? "無法載入語系檔." : "Can not load language file.");
                    MessageBox.Show(ls_msg);
                }
                else
                {
                    ls_lang = "zh-tw";
                    goto Label;
                }
            }

            if (lrd_rd != null)
            {
                //if (Resources.MergedDictionaries.Count > 0) Resources.MergedDictionaries.Clear();
                Resources.MergedDictionaries.Add(lrd_rd);
                lb_load = true;
            }
            return lb_load;
        }

    }
}
